﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace YRevitJig
{
	public class XCurvesJig : XUiJigBase
	{
		public XCurvesJig(UIDocument uiDoc, bool bGraphicsDoubleBuffer = false) : base(uiDoc)
		{
			this.JigStage = XCurvesJig.EStage.eNoOper;
			this.m_bGraphicsDoubleBuffer = bGraphicsDoubleBuffer;
		}

		public XYZ DragCurves(List<Curve> lstCurve, XYZ ptBase, string strPrompt = "请确定插入点：")
		{
			try
			{
				this.Curves = lstCurve;
				this.BasePoint = ptBase;
				this.JigStage = XCurvesJig.EStage.ePickPoint;
				this.PickPoint = base.UiDoc.Selection.PickPoint(this.BasePoint, base.UiDoc, strPrompt);
			}
			catch
			{
				bool flag = this.m_prompDlg != null && base.CmdEndType == XUiJigBase.ECmdEndType.eEnter;
				if (flag)
				{
					base.CmdEndType = XUiJigBase.ECmdEndType.eOther;
					XYZ xyz = base.Client2Revit(this.m_ptCur);
					string inputValue = this.m_prompDlg.GetInputValue();
					double num = Convert.ToDouble(inputValue) / 304.8;
					XYZ xyz2 = (xyz - this.BasePoint).Normalize();
					this.PickPoint = this.BasePoint + xyz2 * num;
				}
				else
				{
					this.PickPoint = null;
				}
			}
			this.JigStage = XCurvesJig.EStage.eNoOper;
			return this.PickPoint;
		}

		public override void Draw(PaintEventArgs paintEventArgs)
		{
			base.Draw(paintEventArgs);
			bool flag = this.JigStage == XCurvesJig.EStage.ePickPoint;
			if (flag)
			{
				System.Drawing.Point point = base.Revit2Client(this.BasePoint);
				System.Drawing.Point ptCur = this.m_ptCur;
				JigFuncs.Client2Screen(this.m_hMDIActiveChildClientWnd, ref ptCur);
				XYZ xyz = base.Screen2Revit(ptCur);
				XYZ xyz2 = xyz - this.BasePoint;
				Transform transform = Transform.CreateTranslation(xyz2);
				List<XDrawable2D> list = new List<XDrawable2D>();
				foreach (Curve curve in this.Curves)
				{
					bool flag2 = curve is Line;
					if (flag2)
					{
						Line line = curve as Line;
						XYZ xyz3 = line.GetEndPoint(0);
						XYZ xyz4 = line.GetEndPoint(1);
						xyz3 = transform.OfPoint(xyz3);
						xyz4 = transform.OfPoint(xyz4);
						System.Drawing.Point ptStart = base.Revit2Client(xyz3);
						System.Drawing.Point ptEnd = base.Revit2Client(xyz4);
						XLine2D item = new XLine2D(ptStart, ptEnd);
						list.Add(item);
					}
					else
					{
						bool flag3 = curve is Arc;
						if (flag3)
						{
							Arc arc = curve as Arc;
							bool isBound = arc.IsBound;
							if (isBound)
							{
								XYZ xyz5 = arc.GetEndPoint(0);
								XYZ xyz6 = arc.GetEndPoint(1);
								XYZ xyz7 = arc.Evaluate(0.5, true);
								xyz5 = transform.OfPoint(xyz5);
								xyz6 = transform.OfPoint(xyz6);
								xyz7 = transform.OfPoint(xyz7);
								System.Drawing.Point ptStart2 = base.Revit2Client(xyz5);
								System.Drawing.Point ptEnd2 = base.Revit2Client(xyz6);
								System.Drawing.Point ptOnArc = base.Revit2Client(xyz7);
								XArc2D item2 = new XArc2D(ptStart2, ptEnd2, ptOnArc);
								list.Add(item2);
							}
							else
							{
								XYZ xyz8 = arc.Center;
								XYZ xyz9 = xyz8 + base.UiDoc.ActiveView.RightDirection * arc.Radius;
								xyz8 = transform.OfPoint(xyz8);
								xyz9 = transform.OfPoint(xyz9);
								System.Drawing.Point point2 = base.Revit2Client(xyz8);
								System.Drawing.Point pt = base.Revit2Client(xyz9);
								int distance = XDrawable2D.GetDistance(pt, point2);
								XCircle2D item3 = new XCircle2D(point2, distance);
								list.Add(item3);
							}
						}
					}
				}
				Graphics graphics = paintEventArgs.Graphics;
				bool bGraphicsDoubleBuffer = this.m_bGraphicsDoubleBuffer;
				if (bGraphicsDoubleBuffer)
				{
					DllImportManeger.Rect rect;
					DllImportManeger.GetClientRect(this.m_hMDIActiveChildClientWnd, out rect);
					System.Drawing.Rectangle targetRectangle = new System.Drawing.Rectangle(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
					BufferedGraphics bufferedGraphics = BufferedGraphicsManager.Current.Allocate(graphics, targetRectangle);
					Graphics graphics2 = bufferedGraphics.Graphics;
					graphics2.SmoothingMode = SmoothingMode.HighQuality;
					graphics2.PixelOffsetMode = PixelOffsetMode.HighSpeed;
					XGraphics xgraphics = new XGraphics(graphics2, base.JigPen);
					foreach (XDrawable2D xdrawable2D in list)
					{
						xdrawable2D.Draw(xgraphics);
					}
					bufferedGraphics.Render(xgraphics.m_graphics);
					bufferedGraphics.Dispose();
				}
				else
				{
					XGraphics g = new XGraphics(graphics, base.JigPen);
					foreach (XDrawable2D xdrawable2D2 in list)
					{
						xdrawable2D2.Draw(g);
					}
				}
			}
		}

		public override List<Curve> CreateCurves()
		{
			List<Curve> list = new List<Curve>();
			try
			{
				bool flag = this.PickPoint != null;
				if (flag)
				{
					Transform transform = Transform.CreateTranslation(this.PickPoint - this.BasePoint);
					foreach (Curve curve in this.Curves)
					{
						Curve item = curve.CreateTransformed(transform);
						list.Add(item);
					}
				}
			}
			catch
			{
			}
			return list;
		}

		public List<Curve> Curves { get; set; }

		public XYZ BasePoint { get; set; }

		public XYZ PickPoint { get; set; }

		public XCurvesJig.EStage JigStage { get; set; }

		private bool m_bGraphicsDoubleBuffer = false;

		public enum EStage
		{
			eNoOper,
			ePickPoint
		}
	}
}
